/*
 * @Author: lei
 * @Date: 2021-08-04 09:03:33
 * @LastEditTime: 2021-08-04 09:05:24
 * @LastEditors: Please set LastEditors
 * @Description: 判断大小端代码
 * @FilePath: /code/big_small_end.cpp
 */
//强制类型转换
#include <iostream>
using namespace std;
int main()
{
    int a = 0x1234;
    //由于int和char的长度不同，借助int型转换成char型，只会留下低地址的部分
    char c = (char)(a);
    if (c == 0x12)
        cout << "big endian" << endl;
    else if(c == 0x34)
        cout << "little endian" << endl;
}
/*
//巧用union联合体
#include <iostream>
using namespace std;
//union联合体的重叠式存储，endian联合体占用内存的空间为每个成员字节长度的最大值
union endian
{
    int a;
    char ch;
};
int main()
{
    endian value;
    value.a = 0x1234;
    //a和ch共用4字节的内存空间
    if (value.ch == 0x12)
        cout << "big endian"<<endl;
    else if (value.ch == 0x34)
        cout << "little endian"<<endl;
}*/